package com.wngbms.netty._01simple.server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.util.CharsetUtil;

import java.time.LocalDateTime;

/**
 * nioEventLoopGroup-3-1 67491806  2021-04-17T19:56:30.094 channelRead 1111
 * nioEventLoopGroup-3-1 67491806  2021-04-17T19:56:30.098 ReadComplete
 * nioEventLoopGroup-3-2 1959805689  2021-04-17T19:56:32.201 channelRead 222
 * nioEventLoopGroup-3-2 1959805689  2021-04-17T19:56:32.201 ReadComplete
 * nioEventLoopGroup-3-1 67491806  2021-04-17T19:56:35.114 exe1 1111
 * nioEventLoopGroup-3-2 1959805689  2021-04-17T19:56:37.211 exe1 222
 * nioEventLoopGroup-3-1 67491806  2021-04-17T19:56:45.122 exe2 1111
 *
 * nioEventLoopGroup-3-1 190180110  2021-04-17T19:56:45.122 channelRead 3333
 * nioEventLoopGroup-3-1 190180110  2021-04-17T19:56:45.122 ReadComplete
 *
 * nioEventLoopGroup-3-2 1959805689  2021-04-17T19:56:47.214 exe2 222
 *
 * nioEventLoopGroup-3-2 11097600  2021-04-17T19:56:47.214 channelRead 4444
 * nioEventLoopGroup-3-2 11097600  2021-04-17T19:56:47.214 ReadComplete
 * nioEventLoopGroup-3-1 190180110  2021-04-17T19:56:50.131 exe1 3333
 * nioEventLoopGroup-3-2 11097600  2021-04-17T19:56:52.219 exe1 4444
 * nioEventLoopGroup-3-1 190180110  2021-04-17T19:57:00.137 exe2 3333
 * nioEventLoopGroup-3-2 11097600  2021-04-17T19:57:02.232 exe2 4444
 */
public class MyInboundHandlerImpl extends ChannelInboundHandlerAdapter {

    public MyInboundHandlerImpl() {
        System.out.println(Thread.currentThread().getName() + " MyInboundHandlerImpl");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(Thread.currentThread().getName() + " channelActive " + this);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(Thread.currentThread().getName() + " channelInactive " + this);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        String content = ((ByteBuf) msg).toString(CharsetUtil.UTF_8);
        String x = Thread.currentThread().getName() + this + getTime() + "channelRead " + content;
        System.out.print(x);

        ChannelPipeline pipeline = ctx.pipeline();
        ChannelHandler handler = ctx.handler();
        Channel channel = ctx.channel();
        EventLoop eventExecutors = channel.eventLoop();



        eventExecutors.execute(() -> {
            try {
                Thread.sleep(5000);
                String x1 = Thread.currentThread().getName() + this + getTime() + "exe1 " + content;
                System.out.print(x1);
                ctx.writeAndFlush(Unpooled.copiedBuffer(x1, CharsetUtil.UTF_8));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        eventExecutors.execute(() -> {
            try {
                Thread.sleep(10000);
                String x1 = Thread.currentThread().getName() + this + getTime() + "exe2 " + content;
                System.out.print(x1);
                ctx.writeAndFlush(Unpooled.copiedBuffer(x1, CharsetUtil.UTF_8));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        String x = Thread.currentThread().getName() + this + getTime() + "ReadComplete " + "\n";
        System.out.print(x);
        ctx.writeAndFlush(Unpooled.copiedBuffer(x, CharsetUtil.UTF_8));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println(Thread.currentThread().getName() + "\t" + "exceptionCaught" + "\t" + this);
        ctx.close();
    }

    @Override
    public String toString() {
        return " " + hashCode() + " ";
    }

    public static String getTime() {
        LocalDateTime localDateTime = LocalDateTime.now();
        return " " + localDateTime.toString() + " ";
    }
}
